package von.seiji.cn.test;

import cn.hutool.core.io.IoUtil;
import org.junit.Test;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 *
 * @Title: Image.java
 * @Description: OpenCV-4.0.0 测试文件
 * @Package com.xu.test
 * @author: xuhyacinth
 * @date: 2019年5月7日12:13:13
 * @version: V-1.0.0
 * @Copyright: 2019 xuhyacinth
 *
 */
public class 使用OpenCV进行人脸识别 {

	static {
		//在使用OpenCV前必须加载Core.NATIVE_LIBRARY_NAME类,否则会报错
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	}

	public static void main(String[] args) {
		face();
		//body();
	}

	/**
	 * OpenCV-4.0.0 人脸识别
	 * @return: void
	 * @date: 2019年5月7日12:16:55
	 */
	public static void face() {
		// 1 读取OpenCV自带的人脸识别特征XML文件
		//OpenCV 图像识别库一般位于 opencv\sources\data 下面
		CascadeClassifier facebook=new CascadeClassifier("E:\\Seiji926\\software\\OpenVC\\opencv430\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
		// 2 读取测试图片
		Mat image = null;
				//=Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\临时储物库\\图片\\图片\\stranger\\stranger_1.jpg");
		/*try {
			byte[] bytes = IoUtil.readBytes(new FileInputStream("C:\\Users\\Administrator\\Desktop\\临时储物库\\图片\\图片\\stranger\\stranger_1.jpg"));
			ArrayList<Byte> bytes1 = new ArrayList<>();
			Collections.addAll(bytes1,bytes);
		Mat mat1 = Converters.vector_char_to_Mat(new ArrayList<>());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}*/
		try {
			byte[] bytes = IoUtil.readBytes(new FileInputStream("C:\\Users\\Administrator\\Desktop\\临时储物库\\图片\\图片\\stranger\\stranger_1.jpg"));
			 image = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_COLOR);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}


				//image = imdecode(fromfile('中文.jpg', dtype=uint8), -1)
		//cv2.imencode('.jpg', img_data)[1].tofile(file_path)
		// 3 特征匹配
		MatOfRect face = new MatOfRect();
		facebook.detectMultiScale(image, face);
		// 4 匹配 Rect 矩阵 数组
		Rect[] rects=face.toArray();
		System.out.println("匹配到 "+rects.length+" 个人脸");
		// 5 为每张识别到的人脸画一个圈
		for (int i = 0; i < rects.length; i++) {
			Imgproc.rectangle(image,new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 0, 255));
			Imgproc.putText(image,"Face", new Point(rects[i].x, rects[i].y),Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA,false);
		}
		// 6 展示图片
		HighGui.imshow("人脸识别", image);
		HighGui.waitKey(0);
	}


	/**
	 * OpenCV-4.0.0 人脸识别
	 * @return: void
	 * @date: 2019年5月7日12:16:55
	 */
	public static void body() {
		// 1 读取OpenCV自带的人脸识别特征XML文件
		//OpenCV 图像识别库一般位于 opencv\sources\data 下面
		//CascadeClassifier facebook=new CascadeClassifier("E:\\Seiji926\\software\\OpenVC\\opencv430\\sources\\data\\haarcascades\\haarcascade_eye.xml");
		//CascadeClassifier facebook=new CascadeClassifier("src/main/resources/data/haarcascade_fullbody.xml");
		CascadeClassifier facebook=new CascadeClassifier("src/main/resources/data/haarcascade_frontalface_alt.xml");
		// 2 读取测试图片
		Mat image = null;
		//=Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\临时储物库\\图片\\图片\\stranger\\stranger_1.jpg");
		try {
			byte[] bytes = IoUtil.readBytes(new FileInputStream("C:\\Users\\Administrator\\Desktop\\111.jpg"));
			image = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_COLOR);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//Mat image = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\20180428110528140.png");

		//image = imdecode(fromfile('中文.jpg', dtype=uint8), -1)
		//cv2.imencode('.jpg', img_data)[1].tofile(file_path)
		// 3 特征匹配
		MatOfRect face = new MatOfRect();
		facebook.detectMultiScale(image, face);
		// 4 匹配 Rect 矩阵 数组
		Rect[] rects=face.toArray();
		System.out.println("匹配到 "+rects.length+" 个人");
		// 5 为每张识别到的人脸画一个圈
		for (int i = 0; i < rects.length; i++) {
			Imgproc.rectangle(image,new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 255, 0));
			Imgproc.putText(image,"Body", new Point(rects[i].x, rects[i].y),Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA,false);
			Mat mat = new Mat(image, rects[i]);
			HighGui.imshow("人体检测", mat);
			HighGui.waitKey(0);
		}
		// 6 展示图片
		HighGui.imshow("人体检测", image);
		HighGui.waitKey(0);
	}

	@Test
	public void test129() {
		System.out.println(new File("").getAbsolutePath());
		System.out.println(new File("src/main/resources/data").getAbsolutePath());
		System.out.println(new File("src/main/resources/data/haarcascade_fullbody.xml").exists());
	}

}
